﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XingFuStudio.Pioneer.Net;
using XingFuStudio.Pioneer.Entity;
using XingFuStudio.Pioneer.IO;
using FluorineFx.IO;
using XingFuStudio.Pioneer.Applications.City.Config;
using XingFuStudio.Pioneer.Applications.City.Entity;

namespace XingFuStudio.Pioneer.Applications.City.Operation {
    /// <summary>
    /// 送礼相关操作
    /// </summary>
    public class Gift : OPBase{
        /// <summary>
        /// 调用方法的目标类
        /// </summary>
        //private readonly string Target = "CCityGift.callfunc";

        /// <summary>
        /// 创建业务对象
        /// </summary>
        /// <param name="httpManager"></param>
        /// <param name="account"></param>
        /// <param name="city"></param>
        /// <param name="config"></param>
        public Gift(HttpManager httpManager, AccountInfo account, CityInfo city, UserConfigSection config)
            : base(httpManager, account, city, config) {
        }

        /// <summary>
        /// 重写操作方法
        /// </summary>
        public override void Operate() {
            base.Operate();

            string message = String.Empty;
            try {
                //打印日志
                this.DoPrintLog(true, "给好友送礼");

                if (this.account.UserId != "6123357") {
                    this.SendWishElement("6123357", 287, out message);
                    //打印日志
                    this.DoPrintLog(true, message);

                    this.SendMoney("6123357", out message);
                    //打印日志
                    this.DoPrintLog(true, message);
                }
                if (this.account.UserId != "4720077") {
                    this.SendWishElement("4720077", 287, out message);
                    //打印日志
                    this.DoPrintLog(true, message);

                    this.SendMoney("4720077", out message);
                    //打印日志
                    this.DoPrintLog(true, message);
                }

                this.SendGift(new List<string>() { "6123357", "4720077" }, 287, out message);
                //打印日志
                this.DoPrintLog(true, message);

                bool hasGift = this.GetHasReceiveGift();

                if (hasGift) {
                    this.ReceiveAllGift(out message);
                    //打印日志
                    this.DoPrintLog(true, message);
                }
            } catch (Exception ex) {
                this.DoError(ex);
            }
        }

        /// <summary>
        /// 给好友送愿望元素
        /// </summary>
        /// <param name="frinedId">好友ID</param>
        /// <param name="elementId">好友愿望元素ID</param>
        /// <param name="resultMessage">输出参数，用来返回结果信息</param>
        /// <returns></returns>
        public bool SendWishElement(string frinedId, int elementId, out string resultMessage) {
            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCityGiftWish.callfunc", "sendWishElements2Friend",
                frinedId, elementId);

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];
            //获取收获是否成功的状态
            bool result = (string)data["result"] == "succ";
            resultMessage = ((string)data["msg"]).NoHtml();

            return result;
        }

        /// <summary>
        /// 送钱
        /// </summary>
        /// <param name="frinedId">好友ID</param>
        /// <param name="resultMessage">输出参数，用来返回结果信息</param>
        /// <returns></returns>
        public bool SendMoney(string frinedId, out string resultMessage) {
            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCitySendMoney.callfunc", "sendmoney",
                frinedId, "normal", 5000, "sugguest");

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];
            //获取收获是否成功的状态
            bool result = (string)data["result"] == "succ";
            resultMessage = ((string)data["word"]).NoHtml();

            return result;
        }

        /// <summary>
        /// 给好友送礼物
        /// </summary>
        /// <param name="friendIds">要送礼物的好友ID列表</param>
        /// <param name="elementId">礼物元素ID</param>
        /// <param name="resultMessage">输出参数，用来返回结果信息</param>
        /// <returns></returns>
        public bool SendGift(List<string> friendIds, int elementId, out string resultMessage) {
            //声明参数字典
            List<object> friends = new List<object>();
            friends.AddRange(friendIds);

            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCityGiftSend.callfunc", "sendGift2Friends",
                friends, (double)elementId, "");

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];

            //获取收获是否成功的状态
            bool result = (string)data["result"] == "succ";
            resultMessage = ((string)data["msg"]).NoHtml();

            return result;
        }

        /// <summary>
        /// 获取是否收到礼物
        /// </summary>
        /// <returns></returns>
        public bool GetHasReceiveGift() {
            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCityGiftRecv.callfunc", "getRecvList");

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            object[] data = (object[])content["data"];

            return data.Length > 0;
        }

        /// <summary>
        /// 接收全部礼物
        /// </summary>
        /// <param name="resultMessage">输出参数，用来返回结果信息</param>
        /// <returns></returns>
        public bool ReceiveAllGift(out string resultMessage) {
            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCityGiftRecv.callfunc", "recvAllGift");

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data = (Dictionary<string, object>)content["data"];

            //获取收获是否成功的状态
            bool result = (string)data["result"] == "succ";
            resultMessage = ((string)data["msg"]).NoHtml();

            return result;
        }
    }
}
